const log = require("../utils/log");
const ws = require("nodejs-websocket");
const msgCfg = require("../common/MsgCfg");
const CmdZt = require('./proto/CmdZt');
const CmdTz = require('./proto/CmdTz');
const gameZt = require('./gameZt/gameZt');
const gameTz = require('./gameTz/gameTz');
const ModeZt = require('./gameZt/gameZtModel');

class gameMessageMgr{
    constructor(){

    }

    static getInstance(){
        if(!gameMessageMgr.instance){
            gameMessageMgr.instance = new gameMessageMgr();
            return gameMessageMgr.instance;
        }else{
            return gameMessageMgr.instance;
        }
    }

    sendMessage(data, client){
        if(this.proto_type === msgCfg.PROTOTYPE.PROTO_BINARY){
            log.error('======sendMessage binary=======',data);
            client.sendBinary(data);
        }else if(this.proto_type === msgCfg.PROTOTYPE.PROTO_JSON){
            log.error('======sendMessage JSON=======',data.type);
            client.send(JSON.stringify(data));
        }
    }

    connectServer(port){
        log.info('=======connectServer==game=========',port);
        let websocket = ws.createServer((client)=>{
            log.info('====收到客户端发送的消息=====');

            //收到的是json 数据
            client.on('text',(result)=>{
                console.log('====收到客户端发送的消息=text=====',result);
                this.proto_type = msgCfg.PROTOTYPE.PROTO_JSON;
                let msg = JSON.parse(result);
                let ctype = msg.type;
                let data = msg.data;
                log.info('======decodeJson========',ctype);
                this.recvMessage(ctype,data, client);
            });

            client.on('error',(err)=>{
                log.error('=======链接出现错误=========',err);
            });

            client.on('close',()=>{
                let uid = client.pathname
                log.error('=======链接断开=========',uid);
                ModeZt.theme_match_discannect_clear_roomInfo(uid);
            });

            client.on('connection',(conn)=>{
                log.error('=======connection=========');
            });

            client.on('listening',(conn)=>{
                log.error('=======listening=========');
            })
        })
        websocket.listen(port);
        console.log('《游戏》服务端正在运行');
    }

    recvMessage(ctype, data, client) {
        log.info('=========recvMessage==========');
        switch (ctype) {
            case CmdZt.ThemeMatchUserEnter://主题赛----用户进入
                this.onThemeMatchUserEnter(ctype, data, client);
                break;
            case CmdZt.ThemeMatchStarReq: //主题赛---用户准备
                this.onThemeMatchStarReq(ctype, data, client);
                break;
            case CmdZt.ThemeMatchGameAnswerReq://主题赛用户答题
                this.theme_match_user_answer(ctype, data, client);
                break;
            case CmdZt.ThemeMatchGameNextGroupRes: //下一组
                this.theme_match_next_group(ctype, data, client);
                break;
            case CmdZt.ThemeMatchGameRecordReq://主题赛 --当前轮记录
                this.theme_match_game_record(ctype, data, client);
                break;
            case CmdZt.ThemeMatchGetDwReward: //主题赛 ---领取段位奖励
                this.theme_match_get_dw_reward(ctype, data, client);
                break;
            case CmdZt.ThemeMatchAnswerTimeOut: //主题赛 ---用户答题超时
                this.theme_match_answer_time_out(ctype, data, client);
                break;
            case CmdZt.ThemeMatchExit: //主题赛 ---用户退出
                this.theme_match_user_exit(ctype, data, client);
                break;

            case CmdTz.GameSceneTzMsg: //挑战赛--- 场景
                this.tz_scene(ctype, data, client);
                break;
            case CmdTz.StartAnswerTzMsg: //挑战赛 --- 答题
                this.tz_answer(ctype, data, client);
                break;
            case CmdTz.StartAnswerTzTimeOutReq:///挑战赛 --- 答题超时
                this.tz_answer_timeout(ctype, data, client);
                break;
            case CmdTz.StartAnswerTzFuHuoReq: //挑战赛 --- 复活
                this.tz_fuhuo(ctype, data, client);
                break;
            case CmdTz.StartAnswerTzEndGetRewardReq: //挑战赛 ----领取结算奖励
                this.tz_getend_reward(ctype, data, client);
                break;
            default:
                break;
        }
    }

    // 主题赛----用户进入
    onThemeMatchUserEnter(ctype, data, client){
        log.error('========主题赛----用户进入=========',data.userId);
        gameZt.on_user_entet(data,ctype,client);
    }

    // 主题赛---用户准备
    onThemeMatchStarReq(ctype, data, client){
        log.error('==========主题赛---用户准备===========',data.userId);
        gameZt.on_user_reday(data,ctype,client);
    }

    //主题赛用户答题
    async theme_match_user_answer(ctype, data, client){
        log.error('===========主题赛用户答题=============',data.userId);
        await gameZt.on_user_answer(data,ctype,client);
    }

    //请求下一组
    theme_match_next_group(ctype, data, client){
        log.info('=============theme_match_next_group=============',data);
        gameZt.on_next_group(data,ctype,client);
    }

    // 主题赛 --当前轮记录
    theme_match_game_record(ctype, data, client){
        log.info('=============theme_match_game_record=============',data);
        gameZt.on_game_record(data,ctype,client);
    }

    // 主题赛 ---领取段位奖励
    theme_match_get_dw_reward(ctype, data, client){
        log.info('=============theme_match_get_dw_reward=============',data);
        gameZt.on_get_dw_end_reward(data,ctype,client);
    }

    //用户答题超时
    theme_match_answer_time_out(ctype, data, client){
        log.info('=============theme_match_answer_time_out=============',data);
        gameZt.on_user_answer_outtime(data,ctype,client);
    }

    // 主题赛 ---用户退出
    theme_match_user_exit(ctype, data, client){
        gameZt.on_user_exit_game(data,ctype,client);
    }

    //-------------------------------------------------挑战赛---------------------------------------------------
    // 挑战赛--- 场景
    async tz_scene(ctype, data, client){
        await gameTz.on_game_scene(data,ctype,client);
    }

    // 挑战赛 --- 答题
    async tz_answer(ctype, data, client){
        await gameTz.on_user_answer(data,ctype, client);
    }

    async tz_answer_timeout(ctype, data, client){
        await gameTz.on_user_answer_timeout(ctype, data, client);
    }

    // 挑战赛 --- 复活
    async tz_fuhuo(ctype, data, client){
        await gameTz.on_user_fuhuo(data,ctype, client);
    }

    //挑战赛---领取结算奖励
    async tz_getend_reward(ctype, data, client){
        await gameTz.on_user_getenRed(data,ctype, client);
    }
}

module.exports = gameMessageMgr.getInstance();